package _90nai.com;

import com.zhu.spider.ExcelExport;
import com.zhu.spider.ExcelExportInterface;
import cpm.haose.video.VideoDetail;
import okhttp3.*;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.processor.PageProcessor;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class Main   implements PageProcessor {

    private Site site = Site.me().setRetryTimes(3).setSleepTime(1000).setTimeOut(10000)
            .addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36");
    public List<String> detailUrls = new ArrayList<>();
    public List<VideoDetail> videoDetails = new ArrayList<>();
    public int pageCount;
    public String host = "http://90nai.com/";
    String search;
    static String basePackage = "d:\\video";

    public Main(String search) {
        this.search = search;
    }

    public static void main(String[] args) throws InterruptedException {
        searchExport("3D");
        downVideo("3D");

    }
    @Override
    public void process(Page page) {
        String url = page.getRequest().getUrl();
        if (url.endsWith("#pageSize")) {
            //获取总页数
            List<String> x = page.getHtml().xpath("//*[@class=\"pagination1\"]/li").all();
            String s = x.get(x.size() - 2);
            pageCount = Integer.parseInt(s.split("<li class=\"page-item\"><a href=\"search-")[1].split(".htm")[0]);
        } else if (url.endsWith("#page")) {
            //每页中的详细页内容
            List<String> detailList = page.getHtml().xpath("//*[@class=\"image\"]/a/@href").all();
            for (String detailUrl : detailList) {
                if (!detailUrl.startsWith("/")) {
                    detailUrls.add(host + "/" + detailUrl + "#detail");
                }
            }
        } else if (url.endsWith("#detail")) {
            //获取详情页中的视频链接
            try {
                String downLoadUrl = "https:" + page.getHtml().xpath("//*[@class=\"panel-body\"]/div/a/@href").get();
                String time = page.getHtml().xpath("//*[@class=\"panel-body\"]/div/text()").all().get(0).split("时长： ")[1].trim();
                String playCount = page.getHtml().xpath("//*[@class=\"panel-body\"]/div/text()").all().get(1).split("播放： ")[1].trim();
                String name = page.getHtml().xpath("//*[@class=\"panel-title\"]/text()").get();
                String thumb = host + "/thumb" + page.getHtml().xpath("//*[@id=\"my_video\"]").get().split("\" webkit-playsinline")[0].split("/thumb")[1];
                videoDetails.add(new VideoDetail(downLoadUrl,time,playCount,name,thumb));
            }catch (Exception e){
                System.out.println("异常页面:"+url);
            }
        }
    }



    private static OkHttpClient okHttpClient = new OkHttpClient();


    private static void searchExport(String search){
        _90nai.com.Main spiderClient = new _90nai.com.Main(search);
        spiderClient.getPageSize();
        spiderClient.getAllDetailPage();
        spiderClient.getVideoAddress();
        spiderClient.outToExcel();
    }

    public void  getVideoAddress(){
        String[] urls = new String[detailUrls.size()];
        int i =0;
        for (String detailUrl:detailUrls){
            urls[i] = detailUrl;
            i++;
        }
        Spider.create(this).addUrl(urls).thread(20).run();
    }
    public  void getPageSize(){
        String page = "https://haosevideo.gq/search-1.htm?label=video&search="+search+"#pageSize";
        Spider.create(this).addUrl(page).thread(20).run();
    }
    public void getAllDetailPage(){
        String[] pages = new String[pageCount];
        for (int i = 0; i < pageCount; i++) {
            pages[i] = "https://haosevideo.gq/search-"+(i+1)+".htm?label=video&search="+search+"#page";
        }
        Spider.create(this).addUrl(pages).thread(20).run();
    }

    @Override
    public Site getSite() {
        return site;
    }
    private static void downVideo(String searchName){
        ExcelExport excelExport = new ExcelExport();
        List<VideoDetail> videoDetails = excelExport.getVideoDetail(basePackage+"\\"+searchName+".xls");
        okHttpClient.dispatcher().setMaxRequests(5);
        okHttpClient.dispatcher().setMaxRequestsPerHost(5);
        for (VideoDetail videoDetail: videoDetails) {
            String path = videoDetail.downLoadUrl;
            Request request = new Request.Builder()
                    .url(path)
                    .build();
            Call call = okHttpClient.newCall(request);
            call.enqueue(new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                    System.out.println(e.getMessage());
                }

                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    InputStream inputStream = response.body().byteStream();
                    DataInputStream dataInputStream = new DataInputStream(inputStream);
                    File file=new File(basePackage+"\\"+searchName);    //设置下载路径
                    if(!file.isDirectory()){
                        file.mkdirs();
                    }
                    FileOutputStream fileOutputStream = new FileOutputStream(new File(file,videoDetail.name+".mp4"));
                    byte[] buffer = new byte[1024];
                    int length;
                    while ((length = dataInputStream.read(buffer)) > 0) {
                        fileOutputStream.write(buffer, 0, length);
                    }
                    dataInputStream.close();
                    fileOutputStream.close();

                }
            });
        }




    }
    void outToExcel(){
        ExcelExportInterface excelExport = new ExcelExport(basePackage+"\\"+search+".xls");
        excelExport.setRowContent(new String[]{"片名","时间","热度","预览图","下载地址"});
        for (VideoDetail videoDetail:videoDetails){
            String[] arr = new String[5];
            arr[0] = videoDetail.name;
            arr[1] = videoDetail.time;
            arr[2] = videoDetail.playCount;
            arr[3] = videoDetail.thumb;
            arr[4] = videoDetail.downLoadUrl;
            excelExport.setRowContent(arr);
        }
        excelExport.exportContent();
    }
}
